#include "mysql_build.h"

//检查数据库是否存在
int databaseExit(MYSQL *mysql,const char * databaseName) {
    char buf[256];
    snprintf(buf,sizeof(buf),"SHOW DATABASES LIKE '%s'",databaseName);

    if(mysql_query(mysql,buf)){
        fprintf(stderr,"数据库查询失败：%s\n",mysql_error(mysql));
        return -1;
    }
    
    MYSQL_RES *result =mysql_store_result(mysql);
    if(!result){
        return -1;
    }
    
    int exists = (mysql_num_rows(result) > 0);
    mysql_free_result(result);
    return exists;

}


//检查表是否存在
int tableExits(MYSQL *mysql,const char *tableName) {
    char buf[255];
    snprintf(buf, sizeof(buf),"SHOW TABLES LIKE '%s'",tableName);

    if(mysql_query(mysql,buf)){
        fprintf(stderr,"表查询失败： %s\n",mysql_error(mysql));
        return -1;
    }
    
    //将查询结果从服务器获取并存储在客户端内存中，返回一个结果集指针                                        
    MYSQL_RES * result = mysql_store_result(mysql);
    if(!result){
        return -1;
    }
    
    int exists = (mysql_num_rows(result) > 0);
    mysql_free_result(result);
    return exists;

}


//创建数据库和表
int setupDatabase(MYSQL *mysql, const char *passWord) {
    if(!mysql){
        fprintf(stderr,"数据库初始化失败\n");
        return -1;
    }

    if(!mysql_real_connect(mysql, "localhost", "root", passWord, NULL, 0, NULL, 0)){
        fprintf(stderr,"连接错误：%s\n",mysql_error(mysql));
        mysql_close(mysql);
        return -1;
    }
    
    const char *databaseName = "cloudDisk3";
    int dbExist = databaseExit(mysql,databaseName);
    
    if(dbExist == -1){
        mysql_close(mysql);
        return -2;
    }
    
    if(dbExist){
        printf("数据库已存在\n");
    
        //使用该数据库
        if(mysql_select_db(mysql,databaseName)){
            fprintf(stderr,"选择数据库失败：%s\n",mysql_error(mysql));
            mysql_close(mysql);
            return -2;
        }
    
        //检查表是否存在
        int userTableExists = tableExits(mysql,"user_table");
        int fileTableExists = tableExits(mysql,"file_info");
    
        if(userTableExists == -1 || fileTableExists == -1){
            mysql_close(mysql);
            return -2;
        }
        if(userTableExists && fileTableExists){
            mysql_close(mysql);
            return -1;
        }
    }else{
        //创建数据库
        char buf[256];
        snprintf(buf,sizeof(buf),"CREATE DATABASE %s",databaseName);
    
        if(mysql_query(mysql,buf)){
            fprintf(stderr,"创建数据库失败：%s\n",mysql_error(mysql));
            mysql_close(mysql);
            return -3;
        }
        printf("数据库创建成功\n");
    
        //使用新创建的数据库
        if(mysql_select_db(mysql,databaseName)){
            fprintf(stderr,"选择数据库失败：%s\n",mysql_error(mysql));
            mysql_close(mysql);
            return -2;
        }
    }
    const char *createUserTable = 
        "CREATE TABLE IF NOT EXISTS user_info ("
        "id INT AUTO_INCREMENT,"
        "username VARCHAR(30) NOT NULL,"
        "salt VARCHAR(128) NOT NULL,"
        "encrypted_password VARCHAR(256) NOT NULL,"
        "is_deleted INT DEFAULT 0,"
        "PRIMARY KEY (id),"
        "UNIQUE KEY(username))";
    
    const char *createFileTable = 
        "CREATE TABLE IF NOT EXISTS file_info ("
        "id INT AUTO_INCREMENT,"
        "filename VARCHAR(255) default '/' NOT NULL,"  // 增大长度
        "username VARCHAR(40) NOT NULL,"
        "parent_id INT default -1,"
        "hash VARCHAR(128) default '0' NOT NULL,"
        "type CHAR(1) default 'd' NOT NULL,"
        "path VARCHAR(1024) default '/',"
        "quotenum int default 1 NOT NULL,"
        "PRIMARY KEY(id))";
    
    if(mysql_query(mysql,createUserTable)){
        fprintf(stderr,"创建用户表失败：%s\n",mysql_error(mysql));
        mysql_close(mysql);
        return -4;
    }
    
    if(mysql_query(mysql,createFileTable)){
        fprintf(stderr,"创建文件表失败：%s\n",mysql_error(mysql));
        mysql_close(mysql);
        return -4;
    }
    printf("成功拥有表\n");
    printf("\n");
    
    return 0;

}

